﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Windows;

namespace BAL.EstRec.FDVUndoPublish
{
    public class VMUndoPublish: INotifyPropertyChanged
    {
        System.Timers.Timer t;
        bool _jobDone;
        bool _manualClose;
        string _message;
        string unidVersion;
        UndoPublishFDV objUndoPublish;
        System.Windows.Controls.Label lblStatus;
        public static System.Threading.Thread thrd;
        System.Windows.Controls.Button btnUndoPublish;
        AppConfs cnfs;


        public event PropertyChangedEventHandler PropertyChanged;

        public bool JobDone
        {
            get { return _jobDone; }
            set
            {
                if (value != _jobDone)
                {
                    this._jobDone = value;
                    if (this.PropertyChanged != null)
                    {
                        this.PropertyChanged(this, new PropertyChangedEventArgs("JobDone"));
                    }
                }
            }
        }

        public bool ManualClose
        {
            get { return _manualClose; }
            set
            {
                if (value != _manualClose)
                {
                    this._manualClose = value;
                    if (this.PropertyChanged != null)
                    {
                        this.PropertyChanged(this, new PropertyChangedEventArgs("ManualClose"));
                    }
                }
            }
        }

        public string Message
        {
            get { return _message; }
            set
            {
                if (value != _message)
                {
                    this._message = value;
                    if (this.PropertyChanged != null)
                    {
                        this.PropertyChanged(this, new PropertyChangedEventArgs("Message"));
                    }
                }
            }
        }

        public VMUndoPublish(string unidVersion, System.Windows.Controls.Label lbl, System.Windows.Controls.Button btn)
        {
            this.lblStatus = lbl;
            this.btnUndoPublish = btn;

            cnfs= new AppConfs();
            this._jobDone = false;
            this._manualClose = false;
            this._message = "Deshaciéndo publicación...";
            this.unidVersion = unidVersion.ToString();
            this.objUndoPublish = new UndoPublishFDV(unidVersion.ToString(), cnfs);
            t = new System.Timers.Timer(100);
            t.Enabled = true;
            t.Elapsed += new System.Timers.ElapsedEventHandler(undoPublish);
        }//

        public void undoPublish(Object sender, System.Timers.ElapsedEventArgs args)
        {
            this.t.Enabled = false;
            ((System.Timers.Timer)sender).Stop();
            this.objUndoPublish.UndoFDVPublish();            
            this.JobDone = true;

            VMUndoPublish.thrd = new System.Threading.Thread(() =>
            {
                Action a = () => this.lblStatus.Content=  this.checkIsPublish();
                Application.Current.Dispatcher.BeginInvoke(a, System.Windows.Threading.DispatcherPriority.Background);
            });

            VMUndoPublish.thrd.IsBackground = true;
            VMUndoPublish.thrd.Start();

        }//

        private string checkIsPublish()
        {
            string menasje = "";

            if (!String.IsNullOrEmpty(this.unidVersion))
            {
                try
                {
                    if (!this.objUndoPublish.isPublish())
                    {
                        menasje = "La publicación ya ha sido borrada.";
                        this.btnUndoPublish.IsEnabled = false;
                        this.btnUndoPublish.Visibility = Visibility.Collapsed;
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                    menasje = "No se ha podido verificar el borrado de la publicación.";
                }
            }
            else menasje = "No se ha podido recuperar la versión actual.";

            return menasje;
        }//

        public void start()
        {
            t.Start();
        }



    }//end class
}
